package frame
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.getQualifiedClassName;
	
	import loaderData.LoaderSys;
	
	import ui.control.GuiScrollBar;
	import ui.control.GuiScrollPane;
	import ui.control.GuiTree;
	import ui.control.GuiWindow;

	public class ResFrame extends GuiWindow
	{
		public var tree:GuiTree;
		public var pane:GuiScrollPane;
		
		public var m_bLoading:Boolean = false;
		public var resList:Vector.<Object>;
		public var loadByteList:Vector.<Object> = new Vector.<Object>;
		public var swfList:Vector.<MovieClip> = new Vector.<MovieClip>;
		
		public var classNameList:Vector.<Object> = new Vector.<Object>;
		
		
		public function ResFrame()
		{
			resList = getSwfList("data/res");
			if(resList && resList.length > 0)
			{
				Load();
			}
			
			tree = new GuiTree();
			tree.openImg =  LoaderSys.inst.getBitmapDataByClass("btn_01_1");
			tree.closeImg =LoaderSys.inst.getBitmapDataByClass("btn_01_2");
			
			pane = new GuiScrollPane();
			pane.graphics.lineStyle(1,0x999999);
			pane.graphics.beginFill(0xDFDFDF,0.8);
			pane.graphics.drawRect(0,0,199,499);
			pane.graphics.endFill();
			
			addChild(pane);
			pane.nWidth = 200;
			pane.nHeight = 500;
			pane.isScale = true;
			
			var bar:GuiScrollBar = new GuiScrollBar();
			bar.x = 200 - bar.nWidth - 1;
			bar.y = 1;
			bar.nHeight = 498;
			pane.addChild(bar);
			
			pane.addChild(tree);
			
		}
		
		
		public function getSwfList(strPath:String):Vector.<Object>
		{
			var vec:Vector.<Object> = new Vector.<Object>;
			
			var file:File = File.applicationDirectory.resolvePath(strPath);
			if (!file.exists)
			{
				return null;
			}
			var list:Array = file.getDirectoryListing();
			
			for (var i:int = 0; i < list.length; i++) 
			{
				if(list[i].type == ".swf")
				{
					var byte:ByteArray = new ByteArray();
					var subfile:File = list[i] as File;
					var stream:FileStream = new FileStream();
					stream.open(subfile,FileMode.READ);
					stream.readBytes(byte,0,stream.bytesAvailable);
					stream.close();
					
					var obj:Object = new Object();
					obj.name = subfile.name;
					obj.ba = byte;
					vec.push(obj);
				}
			}
			return vec;
		}
		
		public function readClass(bytes:ByteArray):Vector.<String>
		{
			var vec:Vector.<String> = new Vector.<String>;
			bytes.position = 0;
			bytes.endian=Endian.LITTLE_ENDIAN;
			bytes.writeBytes(bytes,8);
			bytes.uncompress();
			bytes.position=Math.ceil(((bytes[0]>>>3)*4+5)/8)+4;
			while(bytes.bytesAvailable>2){
				var head:int=bytes.readUnsignedShort();
				var size:int=head&63;
				if (size==63)size=bytes.readInt();
				if (head>>6!=76)bytes.position+=size;
				else {
					head=bytes.readShort();
					for(var i:int=0;i<head;i++){
						bytes.readShort();
						size=bytes.position;
						while(bytes.readByte()!=0);
						size=bytes.position-(bytes.position=size);
						var str:String = bytes.readUTFBytes(size);
						vec.push(str);
						trace(str);
					}
				}
			}
			
			return vec;
		}
		
		private var m_LoadNum:int = 0;
		public function Load():void
		{
			if(resList.length <=0 )
			{
				loadOver();
				m_bLoading = false;
				return;
			}
			m_bLoading = true;
			
			while(resList.length > 0)
			{
				var obj:Object = resList.shift();
				loadByteList.push(obj);
				
				var ba:ByteArray = obj.ba;
				
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteEvent);
				
				var context:LoaderContext = new LoaderContext();
				context.applicationDomain = ApplicationDomain.currentDomain;
				context.allowCodeImport = true;
				loader.loadBytes(ba as ByteArray, context);
				
				m_LoadNum++;
				
				if(m_LoadNum >= 10)
					break;
			}
			
		}
		
		protected function onCompleteEvent(event:Event):void
		{
			var mc:MovieClip =  event.target.content as MovieClip;
			
			
			var loaderInfo:LoaderInfo = event.target as LoaderInfo;
			var loader:Loader = loaderInfo.loader;
			loader.unload();
			loader = null;
			
			swfList.push(mc);
			
			m_LoadNum --;
			if(m_LoadNum <= 0)
			{
				m_LoadNum = 0;
				Load();
			}
		}
		
		private function loadOver():void
		{
			for (var j:int = 0; j < loadByteList.length; j++) 
			{
				var obj:Object = loadByteList[j];
				
				var data:Object = new Object();
				data.name = obj.name;
				data.list = readClass(obj.ba);
				classNameList.push(data);
			}
			
			updataUI();
		}		
		
		private function updataUI():void
		{
			for (var i:int = 0; i < classNameList.length; i++) 
			{
				var strName:String = classNameList[i].name;
				var vec:Vector.<String> = classNameList[i].list;
				
				tree.addItem(strName, "swfName"+ i);
				
				for (var j:int = 0; j < vec.length; j++) 
				{
					var cl:Class = LoaderSys.inst.getClass(vec[j]);
					if(cl)
					{
						var spr:Sprite;
						var ico:* = new cl();
						var bit:Bitmap = null;
						if(ico is BitmapData)
						{
							spr = new Sprite();
							bit = new Bitmap(ico);
							spr.addChild(bit);
						}
						else if(ico is Sprite)
						{
							spr = ico;
						}
						else
						{
							continue;
						}
						spr.width = 40;
						spr.height = 40;
						tree.addItem(vec[j], "swfName"+ i + "_item"+j, "swfName"+ i, spr, cl);
					}
				}
			}
		}
		
	}
}